module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 1. 删除旧的 features JSON 字段和 billing_cycle ENUM
    await queryInterface.removeColumn('plans', 'features');
    await queryInterface.removeColumn('plans', 'billing_cycle');
    
    // 2. 添加新的具体字段
    await queryInterface.addColumn('plans', 'billing_cycle_code', {
      type: Sequelize.STRING(50),
      allowNull: false,
      defaultValue: 'monthly',
      comment: '计费周期代码，关联字典 billing_cycle',
    });
    
    await queryInterface.addColumn('plans', 'max_projects', {
      type: Sequelize.INTEGER,
      allowNull: true,
      comment: '最大项目数，NULL表示无限制',
    });
    
    await queryInterface.addColumn('plans', 'support_level', {
      type: Sequelize.STRING(50),
      allowNull: false,
      defaultValue: 'basic',
      comment: '支持级别代码，关联字典 support_level',
    });
    
    await queryInterface.addColumn('plans', 'has_api_access', {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: '是否有 API 访问权限',
    });
    
    await queryInterface.addColumn('plans', 'has_custom_domain', {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: '是否支持自定义域名',
    });
    
    await queryInterface.addColumn('plans', 'has_white_label', {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: '是否支持白标',
    });
    
    await queryInterface.addColumn('plans', 'has_sso', {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: '是否支持单点登录',
    });
    
    await queryInterface.addColumn('plans', 'has_advanced_analytics', {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: '是否支持高级分析',
    });
    
    // 3. 添加索引
    await queryInterface.addIndex('plans', ['billing_cycle_code'], {
      name: 'plans_billing_cycle_code_idx',
    });
    
    await queryInterface.addIndex('plans', ['support_level'], {
      name: 'plans_support_level_idx',
    });
  },

  down: async (queryInterface, Sequelize) => {
    // 恢复旧结构
    await queryInterface.removeColumn('plans', 'has_advanced_analytics');
    await queryInterface.removeColumn('plans', 'has_sso');
    await queryInterface.removeColumn('plans', 'has_white_label');
    await queryInterface.removeColumn('plans', 'has_custom_domain');
    await queryInterface.removeColumn('plans', 'has_api_access');
    await queryInterface.removeColumn('plans', 'support_level');
    await queryInterface.removeColumn('plans', 'max_projects');
    await queryInterface.removeColumn('plans', 'billing_cycle_code');
    
    await queryInterface.addColumn('plans', 'billing_cycle', {
      type: Sequelize.ENUM('monthly', 'yearly'),
      allowNull: false,
      defaultValue: 'monthly',
    });
    
    await queryInterface.addColumn('plans', 'features', {
      type: Sequelize.JSON,
      allowNull: false,
    });
  },
};

